<?php 
namespace app\index\validate;
use think\Validate;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 店铺验证器
 */
class Shops extends Validate{
	protected $rule = [
	    ['shopSn','require|max:20|checkShopSn:1','请输入店铺编号|店铺编号不能超过20个字符'],
	    ['shopName'  ,'require|max:40','请输入店铺名称|店铺名称不能超过20个字符'],
        ['telephone'  ,'require|max:40','请输入店铺联系电话|店铺联系电话不能超过20个字符'],
        ['areaId'  ,'require','请选择店铺所在区域'],
        ['shopAddress'  ,'require','请输入店铺详细地址'],
        ['shopStatus'  ,'in:0,1','无效的店铺状态']
    ];

    protected $scene = [
        'add'   =>  ['shopSn','shopName','telephone','areaId','shopAddress'],
        'edit'  =>  ['shopSn','shopName','telephone','areaId','shopAddress']
    ]; 
    
    protected function checkShopSn($value){
        $where = [];
        $where['shopSn'] = $value;
        $shopId = Input('post.shopId/d',0);
        if($shopId)$where['shopId'] = ['neq',$shopId];
        $rs = Db::name('Shops')->where($where)->count();
        return ($rs==0)?true:'该店铺编号已存在';
    }
}